package cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.HomeBean;
import cn.rongdao.jrkabao.ui.cashadvance.addcard.AddCreditCardForCashActivity;
import cn.rongdao.jrkabao.ui.cashadvance.addcard.AddDepositCardActivity;
import cn.rongdao.jrkabao.ui.identify.ProgressValidateIdCardActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.LoginUtil;

/**
 * Created by jack on 2018/3/13
 */

public class HomeIdentityUIComponent extends LinearLayout {
    public HomeIdentityUIComponent(Context context) {
        super(context);
    }

    public HomeIdentityUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeIdentityUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView tvTitle;
    private ImageView ivIdentity;
    private boolean isIdentify;
    private boolean isCredit;
    private boolean isBank;
    private boolean isPair;

    private OnHomeLivenessListener onHomeLivenessListener;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvTitle = findViewById(R.id.home_item_title);
        ivIdentity = findViewById(R.id.lci_iv_identity);

        ivIdentity.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (LoginUtil.isLogin(getContext())) {
                    validateInfo();
                }
            }
        });
    }

    private boolean isValidateIdCard() {
        if (!isIdentify) {   //没实名
            ProgressValidateIdCardActivity.start(getContext(), isPair);
            return false;
        }
        if (!isPair) {  //没有进行活体
            if (null != onHomeLivenessListener) {
                onHomeLivenessListener.onLiveness();
            }
            return false;
        }
        return true;
    }

    private void validateInfo() {
        if (isValidateIdCard()) {
            if (!isCredit && !isBank) {
                //实名但没绑卡，跳转到绑信用卡界面
                AddCreditCardForCashActivity.start(getContext(), true, false, "", true);
                return;
            }
            if (!isCredit) {
                //实名且绑了储蓄卡，跳转到绑信用卡界面
                AddCreditCardForCashActivity.start(getContext(), true, true, "", true);
                return;
            }
            if (!isBank) {
                //实名且绑了信用卡，跳转到绑储蓄卡界面
                AddDepositCardActivity.start(getContext(), true, true, "", true);
            }
        }
    }

    public void setContent(HomeBean.UserInfoBean userInfoBean) {
        if (null != userInfoBean) {
            isIdentify = CommonUtil.isStatusSuccess(userInfoBean.getIs_identity());
            isCredit = CommonUtil.isStatusSuccess(userInfoBean.getIs_credit());
            isBank = CommonUtil.isStatusSuccess(userInfoBean.getIs_bank());
            isPair = CommonUtil.isStatusSuccess(userInfoBean.getIs_pair());
            tvTitle.setText(FRString.valueOf(userInfoBean.getTitle()));
            FRImageLoad.loadImage(FRString.valueOf(userInfoBean.getPic()), ivIdentity);
        }
    }

    public void setOnHomeLivenessListener(OnHomeLivenessListener onHomeLivenessListener) {
        this.onHomeLivenessListener = onHomeLivenessListener;
    }

    public interface OnHomeLivenessListener {
        void onLiveness();
    }
}